﻿using System;
using System.ComponentModel;
using System.Web.UI;
using NetDon.Utilities;


namespace NetDon.eBoss.Portal
{
    [Designer("NetDon.Design.DictionaryDataSourceDesigner,NetDon.Design")]
    public class DictionaryDataSource : DataSourceControl
    {
        private DictionaryDataSourceView _view;
        [TypeConverter("NetDon.Design.DictionaryCategoryNameConverter,NetDon.Design")]
        public string CategoryName
        {
            get
            {
                return (string)ViewState["CategoryName"]??string.Empty;
            }
            set
            {
                ViewState["CategoryName"] = value;
            }
        }


        //[TypeConverter("NetDon.Design.DictionaryAreaIdConverter,NetDon.Design")]
        //public Guid AreaId
        //{
        //    get
        //    {
        //        if (ViewState["AreaId"] == null)
        //            return Guid.Empty;
        //        return ViewState["AreaId"].ToString().ToGuid();
        //    }
        //    set
        //    {
        //        ViewState["AreaId"] = value;
        //    }
        //}


        [DefaultValue(ListEmptyType.None)]
        public ListEmptyType EmptyItemType
        {
            get
            {
                object o = ViewState["EmptyItemType"];
                if (o==null)
                {
                    return ListEmptyType.None;
                }
                return (ListEmptyType)o;
            }
            set
            {
                ViewState["EmptyItemType"] = value;
            }
        }

        protected override DataSourceView GetView(string viewName)
        {
            if ((viewName == null) ||
                ((viewName.Length != 0) && !string.Equals(viewName, "DefaultView", StringComparison.OrdinalIgnoreCase)))
            {
                throw new ArgumentException(
                    string.Format(
                        "The data source '{0}' only supports a single view named '{1}'. You may also leave the view name (also called a data member) empty for the default view to be chosen.",
                        ID, "DefaultView"), "viewName");
            }
            return GetView();
        }

        private DictionaryDataSourceView GetView()
        {
            if (_view == null)
            {
                _view = new DictionaryDataSourceView(this, "DefaultView");
            }
            return _view;
        }
    }
}
